//
//  YZMessageHelper.m
//  CocoaAsyncSocketClient
//
//  Created by lixiaoyi on 2018/6/22.
//  Copyright © 2018年 lixiaoyi. All rights reserved.
//

#import "YZMessageHelper.h"
#import "YZMessageConst.h"
#import "YZMessage.h"
#import "YZMessageModel.h"
#import "YZMessageFrame.h"
#import "ImMessage.pbobjc.h"
#import "NSString+YZExtension.h"
#import "YZChatUtils.h"

@implementation YZMessageHelper

+ (YZMessageFrame *)createMessageFrame:(NSString *)type
                             imMessage:(ImMessage *)yzImMessage
                                  path:(NSString *)path
                               fileKey:(NSString *)fileKey
                              isSender:(BOOL)isSender
                 rececivedSenderbySelf:(BOOL)rececivedSenderbySelf                                      senderAvatar:(NSString *)senderAvatar
                        receiverAvatar:(NSString *)receiverAvatar{
    
    YZMessageModel *model = [[YZMessageModel alloc] init];
    model.isSender = isSender;
    model.mediaPath = path;
    model.senderAvatar = senderAvatar;
    model.receiverAvatar = receiverAvatar;
    
    YZMessage *message = [[YZMessage alloc] init];
    message.protocol = yzImMessage.protocol;
    message.version = yzImMessage.version;
    message.messageId = yzImMessage.messageId;
    message.ctrlMessageId = yzImMessage.ctrlMessageId;
    message.messageTime = yzImMessage.messageTime;
    message.messageType = yzImMessage.messageType;
    message.mediaType = yzImMessage.mediaType;
    message.sender = yzImMessage.sender;
    message.receiver = yzImMessage.receiver;
    message.playLoad = yzImMessage.payload;
    message.userId = yzImMessage.userId;
    message.userToken = yzImMessage.userToken;
    message.termType = yzImMessage.termType;
    message.mediaLink = yzImMessage.mediaLink;
    message.appId = yzImMessage.appId;
    message.fileType = yzImMessage.fileType;
    message.fileKey = fileKey;
    message.avatar = yzImMessage.avatar;
    

    type = [self cellTypeWithMessageType:type];
    message.type = type;
    NSString *content = [[NSString alloc] initWithData:message.playLoad encoding:NSUTF8StringEncoding];
    if ([type isEqualToString:TypeText]) {
        message.content = content;
    } else if ([type isEqualToString:TypePic]) {
        message.content = @"[图片]";
    } else if ([type isEqualToString:TypeVoice]) {
        message.content = @"[语音]";
    } else if ([type isEqualToString:TypeVideo]) {
        message.content = @"[视频]";
    } else if ([type isEqualToString:TypeFile]) {
        message.content = content;
    } else if ([type isEqualToString:TypeSystem]) {
//        message.content = [NSString currentMinuteSecond];
//        NSString *intervalStr = [NSString stringWithFormat:@"%.0lld", message.messageTime];
//        message.content = [YZChatUtils timeIntervalToTime:intervalStr];
        message.content = [YZChatUtils showChatTime:message.messageTime];
    } else {
        message.content = content;
    }
    
    if (isSender) {
        message.deliveryState = ICMessageDeliveryState_Delivering;
    } else {
        message.deliveryState = ICMessageDeliveryState_Delivered;
    }
    
    if (rececivedSenderbySelf) {
        message.deliveryState = ICMessageDeliveryState_Delivered;
        model.isSender = YES;
    }
    
    model.message = message;
    YZMessageFrame *modelF = [[YZMessageFrame alloc] init];
    modelF.model = model;
    
    
    return modelF;
}

// 坐标转换到窗口的位置
+ (CGRect)photoFrameInWindow:(UIButton *)photoView {
    return [photoView convertRect:photoView.bounds toView:[UIApplication sharedApplication].keyWindow];
}

// 放大后的图片按钮在窗口的位置
+ (CGRect)photoLargerInWindow:(UIButton *)photoView {
    CGSize  imgSize    = photoView.currentBackgroundImage.size;
    CGFloat appWidth   = [UIScreen mainScreen].bounds.size.width;
    CGFloat appHeight  = [UIScreen mainScreen].bounds.size.height;
    CGFloat height     = imgSize.height / imgSize.width * appWidth;
    CGFloat photoY     = 0;
    if (height < appHeight) {
        photoY         = (appHeight - height) * 0.5;
    }
    return CGRectMake(0, photoY, appWidth, height);
}

// 根据消息类型得到cell的标识
+ (NSString *)cellTypeWithMessageType:(NSString *)type
{
    if ([type isEqualToString:@"1"]) {
        return TypeText;
    } else if ([type isEqualToString:@"2"]) {
        return TypeVoice;
    } else if ([type isEqualToString:@"3"]) {
        return TypePic;
    } else if ([type isEqualToString:@"4"]) {
        return TypeVideo;
    } else if ([type isEqualToString:@"5"]) {
        return TypeFile;
    } else {
        return type;
    }
}

@end
